package pl.edu.pk.mech.kkbus.component;

import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.stereotype.Component;

@Component
public class MyEmailSender {
    
    private static final Logger log = Logger.getLogger(MyEmailSender.class.getName());

    public boolean send(String to, String subject, String message) {
        
        Properties properties = new Properties();
        Session session = Session.getDefaultInstance(properties, null);
        
        try {
            Message mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress("kkbus@example.pl", "KKBus"));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mimeMessage.setSubject(subject);
            mimeMessage.setText(message);
            Transport.send(mimeMessage);
 
            log.info("Success! E-mail send to: " + to);

            return true;
        } catch (Exception e) {
            log.info("Error with sending e-mail!");
        }
        
        return false;
    }
}
